//获取应用实例
const app = getApp()

Page({
    data: {
      boxId:"",
      box:{},
      reportId:'',
      selectedId:"",
      isShowReport:false,
      report:{},
    },

    onLoad: function(options) {
      if (options != null){
        const boxId= options.boxId;
        const score_item= options.score_item;
        this.setData({boxId})
        var that = this;
            wx.setNavigationBarTitle({
              title: score_item
          })
          wx.request({
            url: app.globalData.url+'/wechat/box?boxId='+boxId,
            method: 'get',
            success: res => {
              this.setData({
                box:res.data||{},
                });
            }
          })
          wx.request({
            url: app.globalData.url+'/wechat/student_report',
            data: {
              username: app.globalData.localuserinfo.username,
              boxId: boxId,
              score_item: score_item,
            },
            method: 'POST',
            header: {
              'content-type': 'application/json'
            },
            success: res2 => {
              const report=res2.data;
              console.log('report',report)
              that.setData({report})
              
            }
          })
          }
    },
    iconClick:function(e){
      const that=this;
          // 选择文件，设置上传的类型和数量
          wx.chooseMessageFile({
            count: 1,
            type: 'file',
            success (res) {
              // tempFilePath可以作为 img 标签的 src 属性显示图片
              const tempFilePaths = res.tempFiles;
              const file=tempFilePaths[0];
              const fileName=file.name;
              if(!fileName.endsWith('pdf')){
                wx.showToast({
                  title: '请上传PDF类型文件！',
                  icon: 'none',
                  duration: 2000
                })   
              }else{
                wx.request({
                  url: app.globalData.url + '/wechat/qiniu_upload_token',
                  method: 'get',
                  success: res => {
                    const uploadTask = wx.uploadFile({
                      url: res.data.url,
                      header: { 'content-type': 'multipart/form-data' },
                      method: 'POST',
                      filePath: file.path,
                      name: 'file',
                      formData: {
                        'token': res.data.upload_token
                      },
                      success: function (res2) {
                        const data = JSON.parse(res2.data);
                        const fileUrl=res.data.qiniu_domain+data.key;
                       wx.request({
                        url: app.globalData.url+'/wechat/update_report',
                        data: {
                          id: that.data.report.objectId,
                          key:"fileUrl",
                          value:fileUrl,
                          token:app.globalData.token
                        },
                      method: 'POST',
                       header: {
                        'content-type': 'application/json'
                       },
                      success: res2 => {
                        const report=res2.data;
                       that.setData({report})
                    }
                  })
                      },
                      fail: function (err) {
                        wx.showToast({
                          title: '上传失败，图片服务器出错：' + err.errMsg,
                          icon: 'none',
                          duration: 2000
                        })
                      
                      },
                    })
                    uploadTask.onProgressUpdate((res) => {
                      if (res.progress == 100) {
                        wx.showToast({
                          title: '上传成功！',
                          icon: 'success',
                          duration: 2000
                        })
                  
                      }
                    })
                  },
                  fail: function (err) {
                    wx.showToast({
                      title: '上传失败，获取token出错：' + err,
                      icon: 'none',
                      duration: 2000
                    })

                  }
                });
              }
            }
          })
      
     
    },

    view_report:function(e){
        this.setData({isShowReport:true})
    },

    showSubmit:function(){
      wx.showModal({
        title: '提示',
        content: '确定提交作业？',
        success: (res) => {
          if (res.confirm) {
            this.submitReport()
          }
        }
      })
    },
    submitReport:function(){
      const that=this;
      const score_item_options=this.data.box.score_item_options||{};
      const allowSubmit=score_item_options[this.data.report.score_item]?.allowSubmit;
      if(!allowSubmit){
        wx.showToast({
          title: '实验项目已关闭提交！',
          icon: 'none',
          duration: 2000
        })   
          return 
      }
      wx.request({
        url: app.globalData.url+'/wechat/update_report',
        data: {
          id: that.data.report.objectId,
          key:"status",
          value:'已提交/未批改',
          token:app.globalData.token
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: res2 => {
          const report=res2.data;
          that.setData({report})
        }
      })
    },
    showRemoveReport:function(){
      wx.showModal({
        title: '提示',
        content: '确定删除作业？',
        success: (res) => {
          if (res.confirm) {
            this.removeReport()
          }
        }
      })
    },

    removeReport:function(){
      const that=this;
      wx.request({
        url: app.globalData.url+'/wechat/remove_report',
        data: {
          reportId: that.data.report.objectId,
          token:app.globalData.token
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: res2 => {
          const report=res2.data;
          this.setData({report})

        }
      })
    },

    backReport:function(){
      wx.showModal({
        title: '提示',
        content: '确定撤回该报告？',
        success: (res) => {
          if (res.confirm) {
            this.withdrawReport()
          }
        }
      })
    },
    withdrawReport:function(){
      const that=this;
      wx.request({
        url: app.globalData.url+'/wechat/update_report',
        data: {
          key: 'status',
          value: '草稿',
          id: that.data.report.objectId,
          token:app.globalData.token
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: res => {
          const report=res.data;
          that.setData({report})
        }
      })
    }
})
